<?php

/**
 * A TinyMce Decorator for Oxid's Textareas
 * 
 * @author Ivan I. Atanasov <i.atanasov@viscomp.bg>
 */
class VS_TinyMce {

    /**
     * Relative path to root folder of the editor
     * 
     * @var string
     */
    private $_sEditorPath = NULL;
    
    /**
     * Http url to root folder of the editor
     * 
     * @var string
     */
    private $_sEeditorURL = NULL;
    
    /**
     * Image Paths
     * 
     * @var string
     */
    private $_sImageDir = '';
    private $_sImageURL = '';
    
    /**
     * Alowed image extensions comma-separated ...
     * 
     * @var string
     */
    private $_sAllowedImageExtensions = '';
    
    /**
     * Content Styles
     * 
     * @var string
     */
    private $_sCssUrl = NULL;
    
    
    private $_sLang = '';
    
    private $_sName = '';
    private $_sValue = '';
    
    private $_sCharset = 'utf-8';
    
    /**
     * Default Constructor
     */
    function __construct($sField='tmceEdit', $iLang = 0) {
        
        // set name
        $this->_sName = $sField;

        
        /*
        // Editor Path
        $this->_sEditorPath = '/VsTinyMce/tinymce';
        
        // Editor URL
        $sEditorUrl = $oConfig->getConfigParam( 'sShopURL' )."{$sEditorPath}/";
        $this->_sEeditorURL = $sEditorUrl;
        
        // image directory:
        $this->_sImageDir = $oConfig->getPictureDir( false ).'tinymceoxid/';
        $this->_sImageURL = $oConfig->getPictureUrl( null, false ).'tinymceoxid/';
        
        // set language file name       
        $this->_sLang = $oLang->translateString( 'editor_language', $oLang->getTplLanguage() );
        */
    }
    //===================================================================
    
    /**
     * Fetch Editor's HTML Code
     * 
     * @param int $iWidth
     * @param int $iHeight
     * 
     * @return string	Editor's HTML code
     */
    function fetch($iWidth, $iHeight) {
        //$oLang = oxLang::getInstance();
        //$sLangAbbr = $oLang->getLanguageAbbr($oLang->getTplLanguage());
        $sLangAbbr = 'en';
        
        ob_start();
        include dirname(__FILE__).DIRECTORY_SEPARATOR.'VsTinyMceLoader.php';    // Load TinyMce Jscript and init it
        include dirname(__FILE__).DIRECTORY_SEPARATOR.'VsTinyMceTextarea.php';  // Create an Instance of TinyMce
        $html = ob_get_clean();                                               // Fetch the html code
        
        return $html;
    }
    //===================================================================
    
    /**
     * Set textarea content
     * 
     * @param string $sValue
     */
    function setValue($sValue) {
    	/*
    	 * Reencoding charset to utf8 is performing where it is loading by CURL
    	 */
    	//preg_match('~charset=([-a-z0-9_]+)~i', $sValue, $matches);
	//preg_match('/^content-type: .+?/.+?;\\s?charset=([^;"\\s]+|"[^;"]+")/i', $sValue, $matches);
    	//$charset = $matches[1];
    	//die(var_dump($charset));
    	
    	//$sValue = iconv($charset, 'UTF-8', $sValue);
    	
    	
        $this->_sValue = $sValue;
    }
    //===================================================================
    
    /**
     * Set alowed image extensions comma-separated
     * 
     * @param string $sAllowedImageExtensions
     */
    function setAllowedImageExtensions($sAllowedImageExtensions) {
        $this->_sAllowedImageExtensions = $sAllowedImageExtensions;
    }
    //===================================================================
    
    /**
     * Set content css file
     * 
     * @param string $sCssUrl
     */
    function setStylesheet($sCssUrl) {
        $this->_sCssUrl = $sCssUrl;
    }
    //===================================================================
    
}
